﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EBA.IoC;
using System.Collections.Concurrent;
using System.ComponentModel.Composition;

namespace EBA.Reporting
{
    /// <summary>
    /// 报表引擎
    /// </summary>
    public class ReportEngine
    {
        static ConcurrentDictionary<string, IReportTemplate> ReportTemplates = new ConcurrentDictionary<string, IReportTemplate>();

        [Import]
        IReportTemplateProvider Provider { get; set; }

        /// <summary>
        /// 载入报表模版
        /// </summary>
        /// <param name="id">报表标识</param>
        /// <returns></returns>
        public IReportTemplate LoadTemplate(string id)
        {
            IReportTemplate template;

            if (ReportTemplates.TryGetValue(id, out template) == false)
            {
                var found = this.Provider.ReportTemplates.AsQuerybale
                   .Where(i => i.Id == id)
                   .FirstOrDefault();

                if (found == null)
                {
                    throw new ReportTemplateNotFoundException(id);
                }

                template = found.Clone() as IReportTemplate;

                ReportTemplates.TryAdd(id, template);

            }

            return template;
        }
    }
}
